<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en"><head>

	
		<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
		<meta http-equiv="Content-Language" content="en-us">

		<title>Django | The Forms API | Django Documentation</title>

		<meta name="ROBOTS" content="ALL">
		<meta http-equiv="imagetoolbar" content="no">
		<meta name="MSSmartTagsPreventParsing" content="true">
		<meta name="Copyright" content="This site's design and contents Copyright (c) 2005  Lawrence Journal-World.">

		<meta name="keywords" content="Python, Django, framework, open-source">
		<meta name="description" content="Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design.">

		<link href="Django%20%7C%20The%20Forms%20API%20%7C%20Django%20Documentation_files/base.css" rel="stylesheet" type="text/css" media="screen">
		<link href="Django%20%7C%20The%20Forms%20API%20%7C%20Django%20Documentation_files/print.css" rel="stylesheet" type="text/css" media="print">
      
  
  <style type="text/css" media="screen">
    #docs-search {
      color: #000;
      float: right;
    }
    #docs-search form {
      font-size: 92%;
      margin: 0;
      padding: 1em 1em 0;
      white-space: nowrap;
    }
    form.search ul {
      list-style: none;
      margin: 0;
      padding: 0;
    }
    form.search li {
      display: inline;
      padding-right: 1em;
    }
    form.search .query {
      width: 18em;
    }
  </style>
  <link rel="stylesheet" href="Django%20%7C%20The%20Forms%20API%20%7C%20Django%20Documentation_files/pygments.css" type="text/css">

	</head><body id="documentation" class="default">

	<div id="container">
		<div id="header">
			<h1 id="logo"><a href="http://www.djangoproject.com/"><img src="Django%20%7C%20The%20Forms%20API%20%7C%20Django%20Documentation_files/hdr_logo.gif" alt="Django"></a></h1>
			<ul id="nav-global">
				<li id="nav-homepage"><a href="http://www.djangoproject.com/">Home</a></li>
				<li id="nav-download"><a href="http://www.djangoproject.com/download/">Download</a></li>
				<li id="nav-documentation"><a href="http://docs.djangoproject.com/">Documentation</a></li>
				<li id="nav-weblog"><a href="http://www.djangoproject.com/weblog/">Weblog</a></li>
				<li id="nav-community"><a href="http://www.djangoproject.com/community/">Community</a></li>
				<li id="nav-code"><a href="http://code.djangoproject.com/">Code</a></li>
			</ul>
		</div>
		<!-- END Header -->
		<div id="billboard">
  <h2><a href="http://docs.djangoproject.com/en/1.0/">Django documentation</a></h2>
</div>
		<div id="columnwrap">
			
		<div id="content-main">
		


  <h2 class="deck">
  
    This document describes Django version 1.0. For development documentation, 
    <a href="http://docs.djangoproject.com/en/dev/ref/forms/api/">go here</a>.
  
  </h2>
  <div class="section" id="s-the-forms-api">
<span id="s-ref-forms-api"></span><span id="the-forms-api"></span><span id="ref-forms-api"></span><h1>The Forms API<a class="headerlink" href="#the-forms-api" title="Permalink to this headline">¶</a></h1>
<div class="admonition-about-this-document admonition">
<p class="first admonition-title">About this document</p>
<p class="last">This document covers the gritty details of Django’s forms API. You should
read the <a class="reference external" href="http://docs.djangoproject.com/en/1.0/topics/forms/#topics-forms-index"><em>introduction to working with forms</em></a>
first.</p>
</div>
<div class="section" id="s-bound-and-unbound-forms">
<span id="s-ref-forms-api-bound-unbound"></span><span id="bound-and-unbound-forms"></span><span id="ref-forms-api-bound-unbound"></span><h2>Bound and unbound forms<a class="headerlink" href="#bound-and-unbound-forms" title="Permalink to this headline">¶</a></h2>
<p>A <tt class="xref docutils literal"><span class="pre">Form</span></tt> instance is either <strong>bound</strong> to a set of data, or <strong>unbound</strong>.</p>
<ul class="simple">
<li>If it’s <strong>bound</strong> to a set of data, it’s capable of validating that data
and rendering the form as HTML with the data displayed in the HTML.</li>
<li>If it’s <strong>unbound</strong>, it cannot do validation (because there’s no data to
validate!), but it can still render the blank form as HTML.</li>
</ul>
<p>To create an unbound <tt class="xref docutils literal"><span class="pre">Form</span></tt> instance, simply instantiate the class:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">ContactForm</span><span class="p">()</span>
</pre></div>
</div>
<p>To bind data to a form, pass the data as a dictionary as the first parameter to
your <tt class="xref docutils literal"><span class="pre">Form</span></tt> class constructor:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="p">{</span><span class="s">'subject'</span><span class="p">:</span> <span class="s">'hello'</span><span class="p">,</span>
<span class="gp">... </span>        <span class="s">'message'</span><span class="p">:</span> <span class="s">'Hi there'</span><span class="p">,</span>
<span class="gp">... </span>        <span class="s">'sender'</span><span class="p">:</span> <span class="s">'foo@example.com'</span><span class="p">,</span>
<span class="gp">... </span>        <span class="s">'cc_myself'</span><span class="p">:</span> <span class="bp">True</span><span class="p">}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">ContactForm</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
</pre></div>
</div>
<p>In this dictionary, the keys are the field names, which correspond to the
attributes in your <tt class="xref docutils literal"><span class="pre">Form</span></tt> class. The values are the data you're trying to
validate. These will usually be strings, but there's no requirement that they be
strings; the type of data you pass depends on the <a title="django.forms.Field" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/forms/fields/#django.forms.Field"><tt class="xref docutils literal"><span class="pre">Field</span></tt></a>, as we'll see
in a moment.</p>
<dl class="attribute">
<dt id="django.forms.Form.is_bound">
<!--[django.forms.Form.is_bound]--><tt class="descclassname">Form.</tt><tt class="descname">is_bound</tt><a class="headerlink" href="#django.forms.Form.is_bound" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>If you need to distinguish between bound and unbound form instances at runtime,
check the value of the form's <a title="django.forms.Form.is_bound" class="reference internal" href="#django.forms.Form.is_bound"><tt class="xref docutils literal"><span class="pre">is_bound</span></tt></a> attribute:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">ContactForm</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">is_bound</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">ContactForm</span><span class="p">({</span><span class="s">'subject'</span><span class="p">:</span> <span class="s">'hello'</span><span class="p">})</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">is_bound</span>
<span class="go">True</span>
</pre></div>
</div>
<p>Note that passing an empty dictionary creates a <em>bound</em> form with empty data:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">ContactForm</span><span class="p">({})</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">is_bound</span>
<span class="go">True</span>
</pre></div>
</div>
<p>If you have a bound <tt class="xref docutils literal"><span class="pre">Form</span></tt> instance and want to change the data somehow,
or if you want to bind an unbound <tt class="xref docutils literal"><span class="pre">Form</span></tt> instance to some data, create
another <tt class="xref docutils literal"><span class="pre">Form</span></tt> instance. There is no way to change data in a
<tt class="xref docutils literal"><span class="pre">Form</span></tt> instance. Once a <tt class="xref docutils literal"><span class="pre">Form</span></tt> instance has been created, you
should consider its data immutable, whether it has data or not.</p>
</div>
<div class="section" id="s-using-forms-to-validate-data">
<span id="using-forms-to-validate-data"></span><h2>Using forms to validate data<a class="headerlink" href="#using-forms-to-validate-data" title="Permalink to this headline">¶</a></h2>
<dl class="method">
<dt id="django.forms.Form.is_valid">
<!--[django.forms.Form.is_valid]--><tt class="descclassname">Form.</tt><tt class="descname">is_valid</tt>()<a class="headerlink" href="#django.forms.Form.is_valid" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>The primary task of a <tt class="xref docutils literal"><span class="pre">Form</span></tt> object is to validate data. With a bound
<tt class="xref docutils literal"><span class="pre">Form</span></tt> instance, call the <a title="django.forms.Form.is_valid" class="reference internal" href="#django.forms.Form.is_valid"><tt class="xref docutils literal"><span class="pre">is_valid()</span></tt></a> method to run validation
and return a boolean designating whether the data was valid:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="p">{</span><span class="s">'subject'</span><span class="p">:</span> <span class="s">'hello'</span><span class="p">,</span>
<span class="gp">... </span>        <span class="s">'message'</span><span class="p">:</span> <span class="s">'Hi there'</span><span class="p">,</span>
<span class="gp">... </span>        <span class="s">'sender'</span><span class="p">:</span> <span class="s">'foo@example.com'</span><span class="p">,</span>
<span class="gp">... </span>        <span class="s">'cc_myself'</span><span class="p">:</span> <span class="bp">True</span><span class="p">}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">ContactForm</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">is_valid</span><span class="p">()</span>
<span class="go">True</span>
</pre></div>
</div>
<p>Let's try with some invalid data. In this case, <tt class="docutils literal"><span class="pre">subject</span></tt> is blank (an error,
because all fields are required by default) and <tt class="docutils literal"><span class="pre">sender</span></tt> is not a valid
e-mail address:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="p">{</span><span class="s">'subject'</span><span class="p">:</span> <span class="s">''</span><span class="p">,</span>
<span class="gp">... </span>        <span class="s">'message'</span><span class="p">:</span> <span class="s">'Hi there'</span><span class="p">,</span>
<span class="gp">... </span>        <span class="s">'sender'</span><span class="p">:</span> <span class="s">'invalid e-mail address'</span><span class="p">,</span>
<span class="gp">... </span>        <span class="s">'cc_myself'</span><span class="p">:</span> <span class="bp">True</span><span class="p">}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">ContactForm</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">is_valid</span><span class="p">()</span>
<span class="go">False</span>
</pre></div>
</div>
<dl class="attribute">
<dt id="django.forms.Form.errors">
<!--[django.forms.Form.errors]--><tt class="descclassname">Form.</tt><tt class="descname">errors</tt><a class="headerlink" href="#django.forms.Form.errors" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Access the <a title="django.forms.Form.errors" class="reference internal" href="#django.forms.Form.errors"><tt class="xref docutils literal"><span class="pre">errors</span></tt></a> attribute to get a dictionary of error
messages:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">errors</span>
<span class="go">{'sender': [u'Enter a valid e-mail address.'], 'subject': [u'This field is required.']}</span>
</pre></div>
</div>
<p>In this dictionary, the keys are the field names, and the values are lists of
Unicode strings representing the error messages. The error messages are stored
in lists because a field can have multiple error messages.</p>
<p>You can access <a title="django.forms.Form.errors" class="reference internal" href="#django.forms.Form.errors"><tt class="xref docutils literal"><span class="pre">errors</span></tt></a> without having to call
<a title="django.forms.Form.is_valid" class="reference internal" href="#django.forms.Form.is_valid"><tt class="xref docutils literal"><span class="pre">is_valid()</span></tt></a> first. The form's data will be validated the first time
either you call <a title="django.forms.Form.is_valid" class="reference internal" href="#django.forms.Form.is_valid"><tt class="xref docutils literal"><span class="pre">is_valid()</span></tt></a> or access <a title="django.forms.Form.errors" class="reference internal" href="#django.forms.Form.errors"><tt class="xref docutils literal"><span class="pre">errors</span></tt></a>.</p>
<p>The validation routines will only get called once, regardless of how many times
you access <a title="django.forms.Form.errors" class="reference internal" href="#django.forms.Form.errors"><tt class="xref docutils literal"><span class="pre">errors</span></tt></a> or call <a title="django.forms.Form.is_valid" class="reference internal" href="#django.forms.Form.is_valid"><tt class="xref docutils literal"><span class="pre">is_valid()</span></tt></a>. This means that
if validation has side effects, those side effects will only be triggered once.</p>
<div class="section" id="s-behavior-of-unbound-forms">
<span id="behavior-of-unbound-forms"></span><h3>Behavior of unbound forms<a class="headerlink" href="#behavior-of-unbound-forms" title="Permalink to this headline">¶</a></h3>
<p>It's meaningless to validate a form with no data, but, for the record, here's
what happens with unbound forms:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">ContactForm</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">is_valid</span><span class="p">()</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">errors</span>
<span class="go">{}</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="s-dynamic-initial-values">
<span id="dynamic-initial-values"></span><h2>Dynamic initial values<a class="headerlink" href="#dynamic-initial-values" title="Permalink to this headline">¶</a></h2>
<dl class="attribute">
<dt id="django.forms.Form.initial">
<!--[django.forms.Form.initial]--><tt class="descclassname">Form.</tt><tt class="descname">initial</tt><a class="headerlink" href="#django.forms.Form.initial" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Use <tt class="docutils literal"><span class="pre">initial</span></tt> to declare the initial value of form fields at runtime. For
example, you might want to fill in a <tt class="docutils literal"><span class="pre">username</span></tt> field with the username of the
current session.</p>
<p>To accomplish this, use the <tt class="docutils literal"><span class="pre">initial</span></tt> argument to a <tt class="docutils literal"><span class="pre">Form</span></tt>. This argument,
if given, should be a dictionary mapping field names to initial values. Only
include the fields for which you're specifying an initial value; it's not
necessary to include every field in your form. For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">ContactForm</span><span class="p">(</span><span class="n">initial</span><span class="o">=</span><span class="p">{</span><span class="s">'subject'</span><span class="p">:</span> <span class="s">'Hi there!'</span><span class="p">})</span>
</pre></div>
</div>
<p>These values are only displayed for unbound forms, and they're not used as
fallback values if a particular value isn't provided.</p>
<p>Note that if a <tt class="docutils literal"><span class="pre">Field</span></tt> defines <tt class="docutils literal"><span class="pre">initial</span></tt> <em>and</em> you include <tt class="docutils literal"><span class="pre">initial</span></tt> when
instantiating the <tt class="docutils literal"><span class="pre">Form</span></tt>, then the latter <tt class="docutils literal"><span class="pre">initial</span></tt> will have precedence. In
this example, <tt class="docutils literal"><span class="pre">initial</span></tt> is provided both at the field level and at the form
instance level, and the latter gets precedence:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">CommentForm</span><span class="p">(</span><span class="n">forms</span><span class="o">.</span><span class="n">Form</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">name</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">initial</span><span class="o">=</span><span class="s">'class'</span><span class="p">)</span>
<span class="gp">... </span>    <span class="n">url</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">URLField</span><span class="p">()</span>
<span class="gp">... </span>    <span class="n">comment</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">CharField</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">CommentForm</span><span class="p">(</span><span class="n">initial</span><span class="o">=</span><span class="p">{</span><span class="s">'name'</span><span class="p">:</span> <span class="s">'instance'</span><span class="p">},</span> <span class="n">auto_id</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">f</span>
<span class="go">&lt;tr&gt;&lt;th&gt;Name:&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="name" value="instance" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;Url:&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="url" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;Comment:&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="comment" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="s-accessing-clean-data">
<span id="accessing-clean-data"></span><h2>Accessing "clean" data<a class="headerlink" href="#accessing-clean-data" title="Permalink to this headline">¶</a></h2>
<p>Each <tt class="docutils literal"><span class="pre">Field</span></tt> in a <tt class="docutils literal"><span class="pre">Form</span></tt> class is responsible not only for validating data,
but also for "cleaning" it -- normalizing it to a consistent format. This is a
nice feature, because it allows data for a particular field to be input in
a variety of ways, always resulting in consistent output.</p>
<p>For example, <tt class="docutils literal"><span class="pre">DateField</span></tt> normalizes input into a Python <tt class="docutils literal"><span class="pre">datetime.date</span></tt>
object. Regardless of whether you pass it a string in the format
<tt class="docutils literal"><span class="pre">'1994-07-15'</span></tt>, a <tt class="docutils literal"><span class="pre">datetime.date</span></tt> object or a number of other formats,
<tt class="docutils literal"><span class="pre">DateField</span></tt> will always normalize it to a <tt class="docutils literal"><span class="pre">datetime.date</span></tt> object as long as
it's valid.</p>
<p>Once you've created a <tt class="docutils literal"><span class="pre">Form</span></tt> instance with a set of data and validated it,
you can access the clean data via the <tt class="docutils literal"><span class="pre">cleaned_data</span></tt> attribute of the <tt class="docutils literal"><span class="pre">Form</span></tt>
object:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="p">{</span><span class="s">'subject'</span><span class="p">:</span> <span class="s">'hello'</span><span class="p">,</span>
<span class="gp">... </span>        <span class="s">'message'</span><span class="p">:</span> <span class="s">'Hi there'</span><span class="p">,</span>
<span class="gp">... </span>        <span class="s">'sender'</span><span class="p">:</span> <span class="s">'foo@example.com'</span><span class="p">,</span>
<span class="gp">... </span>        <span class="s">'cc_myself'</span><span class="p">:</span> <span class="bp">True</span><span class="p">}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">ContactForm</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">is_valid</span><span class="p">()</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">cleaned_data</span>
<span class="go">{'cc_myself': True, 'message': u'Hi there', 'sender': u'foo@example.com', 'subject': u'hello'}</span>
</pre></div>
</div>
<div class="versionchanged">
<span class="title">Changed in Django 1.0:</span> The <tt class="docutils literal"><span class="pre">cleaned_data</span></tt> attribute was called <tt class="docutils literal"><span class="pre">clean_data</span></tt> in earlier releases.</div>
<p>Note that any text-based field -- such as <tt class="docutils literal"><span class="pre">CharField</span></tt> or <tt class="docutils literal"><span class="pre">EmailField</span></tt> --
always cleans the input into a Unicode string. We'll cover the encoding
implications later in this document.</p>
<p>If your data does <em>not</em> validate, your <tt class="docutils literal"><span class="pre">Form</span></tt> instance will not have a
<tt class="docutils literal"><span class="pre">cleaned_data</span></tt> attribute:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="p">{</span><span class="s">'subject'</span><span class="p">:</span> <span class="s">''</span><span class="p">,</span>
<span class="gp">... </span>        <span class="s">'message'</span><span class="p">:</span> <span class="s">'Hi there'</span><span class="p">,</span>
<span class="gp">... </span>        <span class="s">'sender'</span><span class="p">:</span> <span class="s">'invalid e-mail address'</span><span class="p">,</span>
<span class="gp">... </span>        <span class="s">'cc_myself'</span><span class="p">:</span> <span class="bp">True</span><span class="p">}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">ContactForm</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">is_valid</span><span class="p">()</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">cleaned_data</span>
<span class="gp">...</span>
<span class="go">AttributeError: 'ContactForm' object has no attribute 'cleaned_data'</span>
</pre></div>
</div>
<p><tt class="docutils literal"><span class="pre">cleaned_data</span></tt> will always <em>only</em> contain a key for fields defined in the
<tt class="docutils literal"><span class="pre">Form</span></tt>, even if you pass extra data when you define the <tt class="docutils literal"><span class="pre">Form</span></tt>. In this
example, we pass a bunch of extra fields to the <tt class="docutils literal"><span class="pre">ContactForm</span></tt> constructor,
but <tt class="docutils literal"><span class="pre">cleaned_data</span></tt> contains only the form's fields:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="p">{</span><span class="s">'subject'</span><span class="p">:</span> <span class="s">'hello'</span><span class="p">,</span>
<span class="gp">... </span>        <span class="s">'message'</span><span class="p">:</span> <span class="s">'Hi there'</span><span class="p">,</span>
<span class="gp">... </span>        <span class="s">'sender'</span><span class="p">:</span> <span class="s">'foo@example.com'</span><span class="p">,</span>
<span class="gp">... </span>        <span class="s">'cc_myself'</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span>
<span class="gp">... </span>        <span class="s">'extra_field_1'</span><span class="p">:</span> <span class="s">'foo'</span><span class="p">,</span>
<span class="gp">... </span>        <span class="s">'extra_field_2'</span><span class="p">:</span> <span class="s">'bar'</span><span class="p">,</span>
<span class="gp">... </span>        <span class="s">'extra_field_3'</span><span class="p">:</span> <span class="s">'baz'</span><span class="p">}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">ContactForm</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">is_valid</span><span class="p">()</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">cleaned_data</span> <span class="c"># Doesn't contain extra_field_1, etc.</span>
<span class="go">{'cc_myself': True, 'message': u'Hi there', 'sender': u'foo@example.com', 'subject': u'hello'}</span>
</pre></div>
</div>
<p><tt class="docutils literal"><span class="pre">cleaned_data</span></tt> will include a key and value for <em>all</em> fields defined in the
<tt class="docutils literal"><span class="pre">Form</span></tt>, even if the data didn't include a value for fields that are not
required. In this example, the data dictionary doesn't include a value for the
<tt class="docutils literal"><span class="pre">nick_name</span></tt> field, but <tt class="docutils literal"><span class="pre">cleaned_data</span></tt> includes it, with an empty value:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">OptionalPersonForm</span><span class="p">(</span><span class="n">Form</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">first_name</span> <span class="o">=</span> <span class="n">CharField</span><span class="p">()</span>
<span class="gp">... </span>    <span class="n">last_name</span> <span class="o">=</span> <span class="n">CharField</span><span class="p">()</span>
<span class="gp">... </span>    <span class="n">nick_name</span> <span class="o">=</span> <span class="n">CharField</span><span class="p">(</span><span class="n">required</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="p">{</span><span class="s">'first_name'</span><span class="p">:</span> <span class="s">u'John'</span><span class="p">,</span> <span class="s">'last_name'</span><span class="p">:</span> <span class="s">u'Lennon'</span><span class="p">}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">OptionalPersonForm</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">is_valid</span><span class="p">()</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">cleaned_data</span>
<span class="go">{'nick_name': u'', 'first_name': u'John', 'last_name': u'Lennon'}</span>
</pre></div>
</div>
<p>In this above example, the <tt class="docutils literal"><span class="pre">cleaned_data</span></tt> value for <tt class="docutils literal"><span class="pre">nick_name</span></tt> is set to an
empty string, because <tt class="docutils literal"><span class="pre">nick_name</span></tt> is <tt class="docutils literal"><span class="pre">CharField</span></tt>, and <tt class="docutils literal"><span class="pre">CharField</span></tt>s treat
empty values as an empty string. Each field type knows what its "blank" value
is -- e.g., for <tt class="docutils literal"><span class="pre">DateField</span></tt>, it's <tt class="xref docutils literal"><span class="pre">None</span></tt> instead of the empty string. For
full details on each field's behavior in this case, see the "Empty value" note
for each field in the "Built-in <tt class="docutils literal"><span class="pre">Field</span></tt> classes" section below.</p>
<p>You can write code to perform validation for particular form fields (based on
their name) or for the form as a whole (considering combinations of various
fields). More information about this is in <a class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/forms/validation/#ref-forms-validation"><em>Form and field validation</em></a>.</p>
</div>
<div class="section" id="s-outputting-forms-as-html">
<span id="outputting-forms-as-html"></span><h2>Outputting forms as HTML<a class="headerlink" href="#outputting-forms-as-html" title="Permalink to this headline">¶</a></h2>
<p>The second task of a <tt class="docutils literal"><span class="pre">Form</span></tt> object is to render itself as HTML. To do so,
simply <tt class="docutils literal"><span class="pre">print</span></tt> it:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">ContactForm</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">f</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_subject"&gt;Subject:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input id="id_subject" type="text" name="subject" maxlength="100" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_message"&gt;Message:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="message" id="id_message" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_sender"&gt;Sender:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="sender" id="id_sender" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_cc_myself"&gt;Cc myself:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="checkbox" name="cc_myself" id="id_cc_myself" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
</pre></div>
</div>
<p>If the form is bound to data, the HTML output will include that data
appropriately. For example, if a field is represented by an
<tt class="docutils literal"><span class="pre">&lt;input</span> <span class="pre">type="text"&gt;</span></tt>, the data will be in the <tt class="docutils literal"><span class="pre">value</span></tt> attribute. If a
field is represented by an <tt class="docutils literal"><span class="pre">&lt;input</span> <span class="pre">type="checkbox"&gt;</span></tt>, then that HTML will
include <tt class="docutils literal"><span class="pre">checked="checked"</span></tt> if appropriate:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="p">{</span><span class="s">'subject'</span><span class="p">:</span> <span class="s">'hello'</span><span class="p">,</span>
<span class="gp">... </span>        <span class="s">'message'</span><span class="p">:</span> <span class="s">'Hi there'</span><span class="p">,</span>
<span class="gp">... </span>        <span class="s">'sender'</span><span class="p">:</span> <span class="s">'foo@example.com'</span><span class="p">,</span>
<span class="gp">... </span>        <span class="s">'cc_myself'</span><span class="p">:</span> <span class="bp">True</span><span class="p">}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">ContactForm</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">f</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_subject"&gt;Subject:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input id="id_subject" type="text" name="subject" maxlength="100" value="hello" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_message"&gt;Message:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="message" id="id_message" value="Hi there" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_sender"&gt;Sender:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="sender" id="id_sender" value="foo@example.com" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_cc_myself"&gt;Cc myself:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="checkbox" name="cc_myself" id="id_cc_myself" checked="checked" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
</pre></div>
</div>
<p>This default output is a two-column HTML table, with a <tt class="docutils literal"><span class="pre">&lt;tr&gt;</span></tt> for each field.
Notice the following:</p>
<ul class="simple">
<li>For flexibility, the output does <em>not</em> include the <tt class="docutils literal"><span class="pre">&lt;table&gt;</span></tt> and
<tt class="docutils literal"><span class="pre">&lt;/table&gt;</span></tt> tags, nor does it include the <tt class="docutils literal"><span class="pre">&lt;form&gt;</span></tt> and <tt class="docutils literal"><span class="pre">&lt;/form&gt;</span></tt>
tags or an <tt class="docutils literal"><span class="pre">&lt;input</span> <span class="pre">type="submit"&gt;</span></tt> tag. It's your job to do that.</li>
<li>Each field type has a default HTML representation. <tt class="docutils literal"><span class="pre">CharField</span></tt> and
<tt class="docutils literal"><span class="pre">EmailField</span></tt> are represented by an <tt class="docutils literal"><span class="pre">&lt;input</span> <span class="pre">type="text"&gt;</span></tt>.
<tt class="docutils literal"><span class="pre">BooleanField</span></tt> is represented by an <tt class="docutils literal"><span class="pre">&lt;input</span> <span class="pre">type="checkbox"&gt;</span></tt>. Note
these are merely sensible defaults; you can specify which HTML to use for
a given field by using widgets, which we'll explain shortly.</li>
<li>The HTML <tt class="docutils literal"><span class="pre">name</span></tt> for each tag is taken directly from its attribute name
in the <tt class="docutils literal"><span class="pre">ContactForm</span></tt> class.</li>
<li>The text label for each field -- e.g. <tt class="docutils literal"><span class="pre">'Subject:'</span></tt>, <tt class="docutils literal"><span class="pre">'Message:'</span></tt> and
<tt class="docutils literal"><span class="pre">'Cc</span> <span class="pre">myself:'</span></tt> is generated from the field name by converting all
underscores to spaces and upper-casing the first letter. Again, note
these are merely sensible defaults; you can also specify labels manually.</li>
<li>Each text label is surrounded in an HTML <tt class="docutils literal"><span class="pre">&lt;label&gt;</span></tt> tag, which points
to the appropriate form field via its <tt class="docutils literal"><span class="pre">id</span></tt>. Its <tt class="docutils literal"><span class="pre">id</span></tt>, in turn, is
generated by prepending <tt class="docutils literal"><span class="pre">'id_'</span></tt> to the field name. The <tt class="docutils literal"><span class="pre">id</span></tt>
attributes and <tt class="docutils literal"><span class="pre">&lt;label&gt;</span></tt> tags are included in the output by default, to
follow best practices, but you can change that behavior.</li>
</ul>
<p>Although <tt class="docutils literal"><span class="pre">&lt;table&gt;</span></tt> output is the default output style when you <tt class="docutils literal"><span class="pre">print</span></tt> a
form, other output styles are available. Each style is available as a method on
a form object, and each rendering method returns a Unicode object.</p>
<div class="section" id="s-as-p">
<span id="as-p"></span><h3><tt class="docutils literal"><span class="pre">as_p()</span></tt><a class="headerlink" href="#as-p" title="Permalink to this headline">¶</a></h3>
<p><tt class="docutils literal"><span class="pre">Form.as_p()</span></tt> renders the form as a series of <tt class="docutils literal"><span class="pre">&lt;p&gt;</span></tt> tags, with each <tt class="docutils literal"><span class="pre">&lt;p&gt;</span></tt>
containing one field:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">ContactForm</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">as_p</span><span class="p">()</span>
<span class="go">u'&lt;p&gt;&lt;label for="id_subject"&gt;Subject:&lt;/label&gt; &lt;input id="id_subject" type="text" name="subject" maxlength="100" /&gt;&lt;/p&gt;\n&lt;p&gt;&lt;label for="id_message"&gt;Message:&lt;/label&gt; &lt;input type="text" name="message" id="id_message" /&gt;&lt;/p&gt;\n&lt;p&gt;&lt;label for="id_sender"&gt;Sender:&lt;/label&gt; &lt;input type="text" name="sender" id="id_sender" /&gt;&lt;/p&gt;\n&lt;p&gt;&lt;label for="id_cc_myself"&gt;Cc myself:&lt;/label&gt; &lt;input type="checkbox" name="cc_myself" id="id_cc_myself" /&gt;&lt;/p&gt;'</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">f</span><span class="o">.</span><span class="n">as_p</span><span class="p">()</span>
<span class="go">&lt;p&gt;&lt;label for="id_subject"&gt;Subject:&lt;/label&gt; &lt;input id="id_subject" type="text" name="subject" maxlength="100" /&gt;&lt;/p&gt;</span>
<span class="go">&lt;p&gt;&lt;label for="id_message"&gt;Message:&lt;/label&gt; &lt;input type="text" name="message" id="id_message" /&gt;&lt;/p&gt;</span>
<span class="go">&lt;p&gt;&lt;label for="id_sender"&gt;Sender:&lt;/label&gt; &lt;input type="text" name="sender" id="id_sender" /&gt;&lt;/p&gt;</span>
<span class="go">&lt;p&gt;&lt;label for="id_cc_myself"&gt;Cc myself:&lt;/label&gt; &lt;input type="checkbox" name="cc_myself" id="id_cc_myself" /&gt;&lt;/p&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="s-as-ul">
<span id="as-ul"></span><h3><tt class="docutils literal"><span class="pre">as_ul()</span></tt><a class="headerlink" href="#as-ul" title="Permalink to this headline">¶</a></h3>
<p><tt class="docutils literal"><span class="pre">Form.as_ul()</span></tt> renders the form as a series of <tt class="docutils literal"><span class="pre">&lt;li&gt;</span></tt> tags, with each
<tt class="docutils literal"><span class="pre">&lt;li&gt;</span></tt> containing one field. It does <em>not</em> include the <tt class="docutils literal"><span class="pre">&lt;ul&gt;</span></tt> or <tt class="docutils literal"><span class="pre">&lt;/ul&gt;</span></tt>,
so that you can specify any HTML attributes on the <tt class="docutils literal"><span class="pre">&lt;ul&gt;</span></tt> for flexibility:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">ContactForm</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">as_ul</span><span class="p">()</span>
<span class="go">u'&lt;li&gt;&lt;label for="id_subject"&gt;Subject:&lt;/label&gt; &lt;input id="id_subject" type="text" name="subject" maxlength="100" /&gt;&lt;/li&gt;\n&lt;li&gt;&lt;label for="id_message"&gt;Message:&lt;/label&gt; &lt;input type="text" name="message" id="id_message" /&gt;&lt;/li&gt;\n&lt;li&gt;&lt;label for="id_sender"&gt;Sender:&lt;/label&gt; &lt;input type="text" name="sender" id="id_sender" /&gt;&lt;/li&gt;\n&lt;li&gt;&lt;label for="id_cc_myself"&gt;Cc myself:&lt;/label&gt; &lt;input type="checkbox" name="cc_myself" id="id_cc_myself" /&gt;&lt;/li&gt;'</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">f</span><span class="o">.</span><span class="n">as_ul</span><span class="p">()</span>
<span class="go">&lt;li&gt;&lt;label for="id_subject"&gt;Subject:&lt;/label&gt; &lt;input id="id_subject" type="text" name="subject" maxlength="100" /&gt;&lt;/li&gt;</span>
<span class="go">&lt;li&gt;&lt;label for="id_message"&gt;Message:&lt;/label&gt; &lt;input type="text" name="message" id="id_message" /&gt;&lt;/li&gt;</span>
<span class="go">&lt;li&gt;&lt;label for="id_sender"&gt;Sender:&lt;/label&gt; &lt;input type="text" name="sender" id="id_sender" /&gt;&lt;/li&gt;</span>
<span class="go">&lt;li&gt;&lt;label for="id_cc_myself"&gt;Cc myself:&lt;/label&gt; &lt;input type="checkbox" name="cc_myself" id="id_cc_myself" /&gt;&lt;/li&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="s-as-table">
<span id="as-table"></span><h3><tt class="docutils literal"><span class="pre">as_table()</span></tt><a class="headerlink" href="#as-table" title="Permalink to this headline">¶</a></h3>
<p>Finally, <tt class="docutils literal"><span class="pre">Form.as_table()</span></tt> outputs the form as an HTML <tt class="docutils literal"><span class="pre">&lt;table&gt;</span></tt>. This is
exactly the same as <tt class="docutils literal"><span class="pre">print</span></tt>. In fact, when you <tt class="docutils literal"><span class="pre">print</span></tt> a form object, it
calls its <tt class="docutils literal"><span class="pre">as_table()</span></tt> method behind the scenes:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">ContactForm</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">as_table</span><span class="p">()</span>
<span class="go">u'&lt;tr&gt;&lt;th&gt;&lt;label for="id_subject"&gt;Subject:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input id="id_subject" type="text" name="subject" maxlength="100" /&gt;&lt;/td&gt;&lt;/tr&gt;\n&lt;tr&gt;&lt;th&gt;&lt;label for="id_message"&gt;Message:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="message" id="id_message" /&gt;&lt;/td&gt;&lt;/tr&gt;\n&lt;tr&gt;&lt;th&gt;&lt;label for="id_sender"&gt;Sender:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="sender" id="id_sender" /&gt;&lt;/td&gt;&lt;/tr&gt;\n&lt;tr&gt;&lt;th&gt;&lt;label for="id_cc_myself"&gt;Cc myself:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="checkbox" name="cc_myself" id="id_cc_myself" /&gt;&lt;/td&gt;&lt;/tr&gt;'</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">f</span><span class="o">.</span><span class="n">as_table</span><span class="p">()</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_subject"&gt;Subject:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input id="id_subject" type="text" name="subject" maxlength="100" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_message"&gt;Message:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="message" id="id_message" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_sender"&gt;Sender:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="sender" id="id_sender" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_cc_myself"&gt;Cc myself:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="checkbox" name="cc_myself" id="id_cc_myself" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="s-configuring-html-label-tags">
<span id="s-ref-forms-api-configuring-label"></span><span id="configuring-html-label-tags"></span><span id="ref-forms-api-configuring-label"></span><h3>Configuring HTML <tt class="docutils literal"><span class="pre">&lt;label&gt;</span></tt> tags<a class="headerlink" href="#configuring-html-label-tags" title="Permalink to this headline">¶</a></h3>
<p>An HTML <tt class="docutils literal"><span class="pre">&lt;label&gt;</span></tt> tag designates which label text is associated with which
form element. This small enhancement makes forms more usable and more accessible
to assistive devices. It's always a good idea to use <tt class="docutils literal"><span class="pre">&lt;label&gt;</span></tt> tags.</p>
<p>By default, the form rendering methods include HTML <tt class="docutils literal"><span class="pre">id</span></tt> attributes on the
form elements and corresponding <tt class="docutils literal"><span class="pre">&lt;label&gt;</span></tt> tags around the labels. The <tt class="docutils literal"><span class="pre">id</span></tt>
attribute values are generated by prepending <tt class="docutils literal"><span class="pre">id_</span></tt> to the form field names.
This behavior is configurable, though, if you want to change the <tt class="docutils literal"><span class="pre">id</span></tt>
convention or remove HTML <tt class="docutils literal"><span class="pre">id</span></tt> attributes and <tt class="docutils literal"><span class="pre">&lt;label&gt;</span></tt> tags entirely.</p>
<p>Use the <tt class="docutils literal"><span class="pre">auto_id</span></tt> argument to the <tt class="docutils literal"><span class="pre">Form</span></tt> constructor to control the label
and <tt class="docutils literal"><span class="pre">id</span></tt> behavior. This argument must be <tt class="xref docutils literal"><span class="pre">True</span></tt>, <tt class="xref docutils literal"><span class="pre">False</span></tt> or a string.</p>
<p>If <tt class="docutils literal"><span class="pre">auto_id</span></tt> is <tt class="xref docutils literal"><span class="pre">False</span></tt>, then the form output will not include <tt class="docutils literal"><span class="pre">&lt;label&gt;</span></tt>
tags nor <tt class="docutils literal"><span class="pre">id</span></tt> attributes:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">ContactForm</span><span class="p">(</span><span class="n">auto_id</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">f</span><span class="o">.</span><span class="n">as_table</span><span class="p">()</span>
<span class="go">&lt;tr&gt;&lt;th&gt;Subject:&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="subject" maxlength="100" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;Message:&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="message" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;Sender:&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="sender" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;Cc myself:&lt;/th&gt;&lt;td&gt;&lt;input type="checkbox" name="cc_myself" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">f</span><span class="o">.</span><span class="n">as_ul</span><span class="p">()</span>
<span class="go">&lt;li&gt;Subject: &lt;input type="text" name="subject" maxlength="100" /&gt;&lt;/li&gt;</span>
<span class="go">&lt;li&gt;Message: &lt;input type="text" name="message" /&gt;&lt;/li&gt;</span>
<span class="go">&lt;li&gt;Sender: &lt;input type="text" name="sender" /&gt;&lt;/li&gt;</span>
<span class="go">&lt;li&gt;Cc myself: &lt;input type="checkbox" name="cc_myself" /&gt;&lt;/li&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">f</span><span class="o">.</span><span class="n">as_p</span><span class="p">()</span>
<span class="go">&lt;p&gt;Subject: &lt;input type="text" name="subject" maxlength="100" /&gt;&lt;/p&gt;</span>
<span class="go">&lt;p&gt;Message: &lt;input type="text" name="message" /&gt;&lt;/p&gt;</span>
<span class="go">&lt;p&gt;Sender: &lt;input type="text" name="sender" /&gt;&lt;/p&gt;</span>
<span class="go">&lt;p&gt;Cc myself: &lt;input type="checkbox" name="cc_myself" /&gt;&lt;/p&gt;</span>
</pre></div>
</div>
<p>If <tt class="docutils literal"><span class="pre">auto_id</span></tt> is set to <tt class="xref docutils literal"><span class="pre">True</span></tt>, then the form output <em>will</em> include
<tt class="docutils literal"><span class="pre">&lt;label&gt;</span></tt> tags and will simply use the field name as its <tt class="docutils literal"><span class="pre">id</span></tt> for each form
field:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">ContactForm</span><span class="p">(</span><span class="n">auto_id</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">f</span><span class="o">.</span><span class="n">as_table</span><span class="p">()</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="subject"&gt;Subject:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input id="subject" type="text" name="subject" maxlength="100" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="message"&gt;Message:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="message" id="message" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="sender"&gt;Sender:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="sender" id="sender" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="cc_myself"&gt;Cc myself:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="checkbox" name="cc_myself" id="cc_myself" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">f</span><span class="o">.</span><span class="n">as_ul</span><span class="p">()</span>
<span class="go">&lt;li&gt;&lt;label for="subject"&gt;Subject:&lt;/label&gt; &lt;input id="subject" type="text" name="subject" maxlength="100" /&gt;&lt;/li&gt;</span>
<span class="go">&lt;li&gt;&lt;label for="message"&gt;Message:&lt;/label&gt; &lt;input type="text" name="message" id="message" /&gt;&lt;/li&gt;</span>
<span class="go">&lt;li&gt;&lt;label for="sender"&gt;Sender:&lt;/label&gt; &lt;input type="text" name="sender" id="sender" /&gt;&lt;/li&gt;</span>
<span class="go">&lt;li&gt;&lt;label for="cc_myself"&gt;Cc myself:&lt;/label&gt; &lt;input type="checkbox" name="cc_myself" id="cc_myself" /&gt;&lt;/li&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">f</span><span class="o">.</span><span class="n">as_p</span><span class="p">()</span>
<span class="go">&lt;p&gt;&lt;label for="subject"&gt;Subject:&lt;/label&gt; &lt;input id="subject" type="text" name="subject" maxlength="100" /&gt;&lt;/p&gt;</span>
<span class="go">&lt;p&gt;&lt;label for="message"&gt;Message:&lt;/label&gt; &lt;input type="text" name="message" id="message" /&gt;&lt;/p&gt;</span>
<span class="go">&lt;p&gt;&lt;label for="sender"&gt;Sender:&lt;/label&gt; &lt;input type="text" name="sender" id="sender" /&gt;&lt;/p&gt;</span>
<span class="go">&lt;p&gt;&lt;label for="cc_myself"&gt;Cc myself:&lt;/label&gt; &lt;input type="checkbox" name="cc_myself" id="cc_myself" /&gt;&lt;/p&gt;</span>
</pre></div>
</div>
<p>If <tt class="docutils literal"><span class="pre">auto_id</span></tt> is set to a string containing the format character <tt class="docutils literal"><span class="pre">'%s'</span></tt>,
then the form output will include <tt class="docutils literal"><span class="pre">&lt;label&gt;</span></tt> tags, and will generate <tt class="docutils literal"><span class="pre">id</span></tt>
attributes based on the format string. For example, for a format string
<tt class="docutils literal"><span class="pre">'field_%s'</span></tt>, a field named <tt class="docutils literal"><span class="pre">subject</span></tt> will get the <tt class="docutils literal"><span class="pre">id</span></tt> value
<tt class="docutils literal"><span class="pre">'field_subject'</span></tt>. Continuing our example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">ContactForm</span><span class="p">(</span><span class="n">auto_id</span><span class="o">=</span><span class="s">'id_for_</span><span class="si">%s</span><span class="s">'</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">f</span><span class="o">.</span><span class="n">as_table</span><span class="p">()</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_for_subject"&gt;Subject:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input id="id_for_subject" type="text" name="subject" maxlength="100" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_for_message"&gt;Message:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="message" id="id_for_message" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_for_sender"&gt;Sender:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="sender" id="id_for_sender" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_for_cc_myself"&gt;Cc myself:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="checkbox" name="cc_myself" id="id_for_cc_myself" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">f</span><span class="o">.</span><span class="n">as_ul</span><span class="p">()</span>
<span class="go">&lt;li&gt;&lt;label for="id_for_subject"&gt;Subject:&lt;/label&gt; &lt;input id="id_for_subject" type="text" name="subject" maxlength="100" /&gt;&lt;/li&gt;</span>
<span class="go">&lt;li&gt;&lt;label for="id_for_message"&gt;Message:&lt;/label&gt; &lt;input type="text" name="message" id="id_for_message" /&gt;&lt;/li&gt;</span>
<span class="go">&lt;li&gt;&lt;label for="id_for_sender"&gt;Sender:&lt;/label&gt; &lt;input type="text" name="sender" id="id_for_sender" /&gt;&lt;/li&gt;</span>
<span class="go">&lt;li&gt;&lt;label for="id_for_cc_myself"&gt;Cc myself:&lt;/label&gt; &lt;input type="checkbox" name="cc_myself" id="id_for_cc_myself" /&gt;&lt;/li&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">f</span><span class="o">.</span><span class="n">as_p</span><span class="p">()</span>
<span class="go">&lt;p&gt;&lt;label for="id_for_subject"&gt;Subject:&lt;/label&gt; &lt;input id="id_for_subject" type="text" name="subject" maxlength="100" /&gt;&lt;/p&gt;</span>
<span class="go">&lt;p&gt;&lt;label for="id_for_message"&gt;Message:&lt;/label&gt; &lt;input type="text" name="message" id="id_for_message" /&gt;&lt;/p&gt;</span>
<span class="go">&lt;p&gt;&lt;label for="id_for_sender"&gt;Sender:&lt;/label&gt; &lt;input type="text" name="sender" id="id_for_sender" /&gt;&lt;/p&gt;</span>
<span class="go">&lt;p&gt;&lt;label for="id_for_cc_myself"&gt;Cc myself:&lt;/label&gt; &lt;input type="checkbox" name="cc_myself" id="id_for_cc_myself" /&gt;&lt;/p&gt;</span>
</pre></div>
</div>
<p>If <tt class="docutils literal"><span class="pre">auto_id</span></tt> is set to any other true value -- such as a string that doesn't
include <tt class="docutils literal"><span class="pre">%s</span></tt> -- then the library will act as if <tt class="docutils literal"><span class="pre">auto_id</span></tt> is <tt class="xref docutils literal"><span class="pre">True</span></tt>.</p>
<p>By default, <tt class="docutils literal"><span class="pre">auto_id</span></tt> is set to the string <tt class="docutils literal"><span class="pre">'id_%s'</span></tt>.</p>
<p>Normally, a colon (<tt class="docutils literal"><span class="pre">:</span></tt>) will be appended after any label name when a form is
rendered. It's possible to change the colon to another character, or omit it
entirely, using the <tt class="docutils literal"><span class="pre">label_suffix</span></tt> parameter:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">ContactForm</span><span class="p">(</span><span class="n">auto_id</span><span class="o">=</span><span class="s">'id_for_</span><span class="si">%s</span><span class="s">'</span><span class="p">,</span> <span class="n">label_suffix</span><span class="o">=</span><span class="s">''</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">f</span><span class="o">.</span><span class="n">as_ul</span><span class="p">()</span>
<span class="go">&lt;li&gt;&lt;label for="id_for_subject"&gt;Subject&lt;/label&gt; &lt;input id="id_for_subject" type="text" name="subject" maxlength="100" /&gt;&lt;/li&gt;</span>
<span class="go">&lt;li&gt;&lt;label for="id_for_message"&gt;Message&lt;/label&gt; &lt;input type="text" name="message" id="id_for_message" /&gt;&lt;/li&gt;</span>
<span class="go">&lt;li&gt;&lt;label for="id_for_sender"&gt;Sender&lt;/label&gt; &lt;input type="text" name="sender" id="id_for_sender" /&gt;&lt;/li&gt;</span>
<span class="go">&lt;li&gt;&lt;label for="id_for_cc_myself"&gt;Cc myself&lt;/label&gt; &lt;input type="checkbox" name="cc_myself" id="id_for_cc_myself" /&gt;&lt;/li&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">ContactForm</span><span class="p">(</span><span class="n">auto_id</span><span class="o">=</span><span class="s">'id_for_</span><span class="si">%s</span><span class="s">'</span><span class="p">,</span> <span class="n">label_suffix</span><span class="o">=</span><span class="s">' -&gt;'</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">f</span><span class="o">.</span><span class="n">as_ul</span><span class="p">()</span>
<span class="go">&lt;li&gt;&lt;label for="id_for_subject"&gt;Subject -&gt;&lt;/label&gt; &lt;input id="id_for_subject" type="text" name="subject" maxlength="100" /&gt;&lt;/li&gt;</span>
<span class="go">&lt;li&gt;&lt;label for="id_for_message"&gt;Message -&gt;&lt;/label&gt; &lt;input type="text" name="message" id="id_for_message" /&gt;&lt;/li&gt;</span>
<span class="go">&lt;li&gt;&lt;label for="id_for_sender"&gt;Sender -&gt;&lt;/label&gt; &lt;input type="text" name="sender" id="id_for_sender" /&gt;&lt;/li&gt;</span>
<span class="go">&lt;li&gt;&lt;label for="id_for_cc_myself"&gt;Cc myself -&gt;&lt;/label&gt; &lt;input type="checkbox" name="cc_myself" id="id_for_cc_myself" /&gt;&lt;/li&gt;</span>
</pre></div>
</div>
<p>Note that the label suffix is added only if the last character of the
label isn't a punctuation character (<tt class="docutils literal"><span class="pre">.</span></tt>, <tt class="docutils literal"><span class="pre">!</span></tt>, <tt class="docutils literal"><span class="pre">?</span></tt> or <tt class="docutils literal"><span class="pre">:</span></tt>)</p>
</div>
<div class="section" id="s-notes-on-field-ordering">
<span id="notes-on-field-ordering"></span><h3>Notes on field ordering<a class="headerlink" href="#notes-on-field-ordering" title="Permalink to this headline">¶</a></h3>
<p>In the <tt class="docutils literal"><span class="pre">as_p()</span></tt>, <tt class="docutils literal"><span class="pre">as_ul()</span></tt> and <tt class="docutils literal"><span class="pre">as_table()</span></tt> shortcuts, the fields are
displayed in the order in which you define them in your form class. For
example, in the <tt class="docutils literal"><span class="pre">ContactForm</span></tt> example, the fields are defined in the order
<tt class="docutils literal"><span class="pre">subject</span></tt>, <tt class="docutils literal"><span class="pre">message</span></tt>, <tt class="docutils literal"><span class="pre">sender</span></tt>, <tt class="docutils literal"><span class="pre">cc_myself</span></tt>. To reorder the HTML
output, just change the order in which those fields are listed in the class.</p>
</div>
<div class="section" id="s-how-errors-are-displayed">
<span id="how-errors-are-displayed"></span><h3>How errors are displayed<a class="headerlink" href="#how-errors-are-displayed" title="Permalink to this headline">¶</a></h3>
<p>If you render a bound <tt class="docutils literal"><span class="pre">Form</span></tt> object, the act of rendering will automatically
run the form's validation if it hasn't already happened, and the HTML output
will include the validation errors as a <tt class="docutils literal"><span class="pre">&lt;ul</span> <span class="pre">class="errorlist"&gt;</span></tt> near the
field. The particular positioning of the error messages depends on the output
method you're using:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="p">{</span><span class="s">'subject'</span><span class="p">:</span> <span class="s">''</span><span class="p">,</span>
<span class="gp">... </span>        <span class="s">'message'</span><span class="p">:</span> <span class="s">'Hi there'</span><span class="p">,</span>
<span class="gp">... </span>        <span class="s">'sender'</span><span class="p">:</span> <span class="s">'invalid e-mail address'</span><span class="p">,</span>
<span class="gp">... </span>        <span class="s">'cc_myself'</span><span class="p">:</span> <span class="bp">True</span><span class="p">}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">ContactForm</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">auto_id</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">f</span><span class="o">.</span><span class="n">as_table</span><span class="p">()</span>
<span class="go">&lt;tr&gt;&lt;th&gt;Subject:&lt;/th&gt;&lt;td&gt;&lt;ul class="errorlist"&gt;&lt;li&gt;This field is required.&lt;/li&gt;&lt;/ul&gt;&lt;input type="text" name="subject" maxlength="100" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;Message:&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="message" value="Hi there" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;Sender:&lt;/th&gt;&lt;td&gt;&lt;ul class="errorlist"&gt;&lt;li&gt;Enter a valid e-mail address.&lt;/li&gt;&lt;/ul&gt;&lt;input type="text" name="sender" value="invalid e-mail address" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;Cc myself:&lt;/th&gt;&lt;td&gt;&lt;input checked="checked" type="checkbox" name="cc_myself" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">f</span><span class="o">.</span><span class="n">as_ul</span><span class="p">()</span>
<span class="go">&lt;li&gt;&lt;ul class="errorlist"&gt;&lt;li&gt;This field is required.&lt;/li&gt;&lt;/ul&gt;Subject: &lt;input type="text" name="subject" maxlength="100" /&gt;&lt;/li&gt;</span>
<span class="go">&lt;li&gt;Message: &lt;input type="text" name="message" value="Hi there" /&gt;&lt;/li&gt;</span>
<span class="go">&lt;li&gt;&lt;ul class="errorlist"&gt;&lt;li&gt;Enter a valid e-mail address.&lt;/li&gt;&lt;/ul&gt;Sender: &lt;input type="text" name="sender" value="invalid e-mail address" /&gt;&lt;/li&gt;</span>
<span class="go">&lt;li&gt;Cc myself: &lt;input checked="checked" type="checkbox" name="cc_myself" /&gt;&lt;/li&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">f</span><span class="o">.</span><span class="n">as_p</span><span class="p">()</span>
<span class="go">&lt;p&gt;&lt;ul class="errorlist"&gt;&lt;li&gt;This field is required.&lt;/li&gt;&lt;/ul&gt;&lt;/p&gt;</span>
<span class="go">&lt;p&gt;Subject: &lt;input type="text" name="subject" maxlength="100" /&gt;&lt;/p&gt;</span>
<span class="go">&lt;p&gt;Message: &lt;input type="text" name="message" value="Hi there" /&gt;&lt;/p&gt;</span>
<span class="go">&lt;p&gt;&lt;ul class="errorlist"&gt;&lt;li&gt;Enter a valid e-mail address.&lt;/li&gt;&lt;/ul&gt;&lt;/p&gt;</span>
<span class="go">&lt;p&gt;Sender: &lt;input type="text" name="sender" value="invalid e-mail address" /&gt;&lt;/p&gt;</span>
<span class="go">&lt;p&gt;Cc myself: &lt;input checked="checked" type="checkbox" name="cc_myself" /&gt;&lt;/p&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="s-customizing-the-error-list-format">
<span id="customizing-the-error-list-format"></span><h3>Customizing the error list format<a class="headerlink" href="#customizing-the-error-list-format" title="Permalink to this headline">¶</a></h3>
<p>By default, forms use <tt class="docutils literal"><span class="pre">django.forms.util.ErrorList</span></tt> to format validation
errors. If you'd like to use an alternate class for displaying errors, you can
pass that in at construction time:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">from</span> <span class="nn">django.forms.util</span> <span class="k">import</span> <span class="n">ErrorList</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">DivErrorList</span><span class="p">(</span><span class="n">ErrorList</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">__unicode__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span>        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">as_divs</span><span class="p">()</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">as_divs</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span>        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="p">:</span> <span class="k">return</span> <span class="s">u''</span>
<span class="gp">... </span>        <span class="k">return</span> <span class="s">u'&lt;div class="errorlist"&gt;</span><span class="si">%s</span><span class="s">&lt;/div&gt;'</span> <span class="o">%</span> <span class="s">''</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="s">u'&lt;div class="error"&gt;</span><span class="si">%s</span><span class="s">&lt;/div&gt;'</span> <span class="o">%</span> <span class="n">e</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="bp">self</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">ContactForm</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">auto_id</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">error_class</span><span class="o">=</span><span class="n">DivErrorList</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">as_p</span><span class="p">()</span>
<span class="go">&lt;div class="errorlist"&gt;&lt;div class="error"&gt;This field is required.&lt;/div&gt;&lt;/div&gt;</span>
<span class="go">&lt;p&gt;Subject: &lt;input type="text" name="subject" maxlength="100" /&gt;&lt;/p&gt;</span>
<span class="go">&lt;p&gt;Message: &lt;input type="text" name="message" value="Hi there" /&gt;&lt;/p&gt;</span>
<span class="go">&lt;div class="errorlist"&gt;&lt;div class="error"&gt;Enter a valid e-mail address.&lt;/div&gt;&lt;/div&gt;</span>
<span class="go">&lt;p&gt;Sender: &lt;input type="text" name="sender" value="invalid e-mail address" /&gt;&lt;/p&gt;</span>
<span class="go">&lt;p&gt;Cc myself: &lt;input checked="checked" type="checkbox" name="cc_myself" /&gt;&lt;/p&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="s-more-granular-output">
<span id="more-granular-output"></span><h3>More granular output<a class="headerlink" href="#more-granular-output" title="Permalink to this headline">¶</a></h3>
<p>The <tt class="docutils literal"><span class="pre">as_p()</span></tt>, <tt class="docutils literal"><span class="pre">as_ul()</span></tt> and <tt class="docutils literal"><span class="pre">as_table()</span></tt> methods are simply shortcuts for
lazy developers -- they're not the only way a form object can be displayed.</p>
<p>To display the HTML for a single field in your form, use dictionary lookup
syntax using the field's name as the key, and print the resulting object:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">ContactForm</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">f</span><span class="p">[</span><span class="s">'subject'</span><span class="p">]</span>
<span class="go">&lt;input id="id_subject" type="text" name="subject" maxlength="100" /&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">f</span><span class="p">[</span><span class="s">'message'</span><span class="p">]</span>
<span class="go">&lt;input type="text" name="message" id="id_message" /&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">f</span><span class="p">[</span><span class="s">'sender'</span><span class="p">]</span>
<span class="go">&lt;input type="text" name="sender" id="id_sender" /&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">f</span><span class="p">[</span><span class="s">'cc_myself'</span><span class="p">]</span>
<span class="go">&lt;input type="checkbox" name="cc_myself" id="id_cc_myself" /&gt;</span>
</pre></div>
</div>
<p>Call <tt class="docutils literal"><span class="pre">str()</span></tt> or <tt class="docutils literal"><span class="pre">unicode()</span></tt> on the field to get its rendered HTML as a
string or Unicode object, respectively:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="nb">str</span><span class="p">(</span><span class="n">f</span><span class="p">[</span><span class="s">'subject'</span><span class="p">])</span>
<span class="go">'&lt;input id="id_subject" type="text" name="subject" maxlength="100" /&gt;'</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">unicode</span><span class="p">(</span><span class="n">f</span><span class="p">[</span><span class="s">'subject'</span><span class="p">])</span>
<span class="go">u'&lt;input id="id_subject" type="text" name="subject" maxlength="100" /&gt;'</span>
</pre></div>
</div>
<p>Form objects define a custom <tt class="docutils literal"><span class="pre">__iter__()</span></tt> method, which allows you to loop
through their fields:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">ContactForm</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">f</span><span class="p">:</span> <span class="k">print</span> <span class="n">field</span>
<span class="go">&lt;input id="id_subject" type="text" name="subject" maxlength="100" /&gt;</span>
<span class="go">&lt;input type="text" name="message" id="id_message" /&gt;</span>
<span class="go">&lt;input type="text" name="sender" id="id_sender" /&gt;</span>
<span class="go">&lt;input type="checkbox" name="cc_myself" id="id_cc_myself" /&gt;</span>
</pre></div>
</div>
<p>The field-specific output honors the form object's <tt class="docutils literal"><span class="pre">auto_id</span></tt> setting:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">ContactForm</span><span class="p">(</span><span class="n">auto_id</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">f</span><span class="p">[</span><span class="s">'message'</span><span class="p">]</span>
<span class="go">&lt;input type="text" name="message" /&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">ContactForm</span><span class="p">(</span><span class="n">auto_id</span><span class="o">=</span><span class="s">'id_</span><span class="si">%s</span><span class="s">'</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">f</span><span class="p">[</span><span class="s">'message'</span><span class="p">]</span>
<span class="go">&lt;input type="text" name="message" id="id_message" /&gt;</span>
</pre></div>
</div>
<p>For a field's list of errors, access the field's <tt class="docutils literal"><span class="pre">errors</span></tt> attribute. This
is a list-like object that is displayed as an HTML <tt class="docutils literal"><span class="pre">&lt;ul</span> <span class="pre">class="errorlist"&gt;</span></tt>
when printed:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="p">{</span><span class="s">'subject'</span><span class="p">:</span> <span class="s">'hi'</span><span class="p">,</span> <span class="s">'message'</span><span class="p">:</span> <span class="s">''</span><span class="p">,</span> <span class="s">'sender'</span><span class="p">:</span> <span class="s">''</span><span class="p">,</span> <span class="s">'cc_myself'</span><span class="p">:</span> <span class="s">''</span><span class="p">}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">ContactForm</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">auto_id</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">f</span><span class="p">[</span><span class="s">'message'</span><span class="p">]</span>
<span class="go">&lt;input type="text" name="message" /&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="p">[</span><span class="s">'message'</span><span class="p">]</span><span class="o">.</span><span class="n">errors</span>
<span class="go">[u'This field is required.']</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">f</span><span class="p">[</span><span class="s">'message'</span><span class="p">]</span><span class="o">.</span><span class="n">errors</span>
<span class="go">&lt;ul class="errorlist"&gt;&lt;li&gt;This field is required.&lt;/li&gt;&lt;/ul&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="p">[</span><span class="s">'subject'</span><span class="p">]</span><span class="o">.</span><span class="n">errors</span>
<span class="go">[]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">f</span><span class="p">[</span><span class="s">'subject'</span><span class="p">]</span><span class="o">.</span><span class="n">errors</span>

<span class="gp">&gt;&gt;&gt; </span><span class="nb">str</span><span class="p">(</span><span class="n">f</span><span class="p">[</span><span class="s">'subject'</span><span class="p">]</span><span class="o">.</span><span class="n">errors</span><span class="p">)</span>
<span class="go">''</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="s-binding-uploaded-files-to-a-form">
<span id="s-binding-uploaded-files"></span><span id="binding-uploaded-files-to-a-form"></span><span id="binding-uploaded-files"></span><h2>Binding uploaded files to a form<a class="headerlink" href="#binding-uploaded-files-to-a-form" title="Permalink to this headline">¶</a></h2>
<div class="versionadded">
<span class="title">New in Django 1.0:</span> <a class="reference external" href="http://docs.djangoproject.com/en/1.0/releases/1.0/#releases-1-0"><em>Please, see the release notes</em></a></div>
<p>Dealing with forms that have <tt class="docutils literal"><span class="pre">FileField</span></tt> and <tt class="docutils literal"><span class="pre">ImageField</span></tt> fields
is a little more complicated than a normal form.</p>
<p>Firstly, in order to upload files, you'll need to make sure that your
<tt class="docutils literal"><span class="pre">&lt;form&gt;</span></tt> element correctly defines the <tt class="docutils literal"><span class="pre">enctype</span></tt> as
<tt class="docutils literal"><span class="pre">"multipart/form-data"</span></tt>:</p>
<div class="highlight-python"><pre>&lt;form enctype="multipart/form-data" method="post" action="/foo/"&gt;</pre>
</div>
<p>Secondly, when you use the form, you need to bind the file data. File
data is handled separately to normal form data, so when your form
contains a <tt class="docutils literal"><span class="pre">FileField</span></tt> and <tt class="docutils literal"><span class="pre">ImageField</span></tt>, you will need to specify
a second argument when you bind your form. So if we extend our
ContactForm to include an <tt class="docutils literal"><span class="pre">ImageField</span></tt> called <tt class="docutils literal"><span class="pre">mugshot</span></tt>, we
need to bind the file data containing the mugshot image:</p>
<div class="highlight-python"><pre># Bound form with an image field
&gt;&gt;&gt; from django.core.files.uploadedfile import SimpleUploadedFile
&gt;&gt;&gt; data = {'subject': 'hello',
...         'message': 'Hi there',
...         'sender': 'foo@example.com',
...         'cc_myself': True}
&gt;&gt;&gt; file_data = {'mugshot': SimpleUploadedFile('face.jpg', &lt;file data&gt;)}
&gt;&gt;&gt; f = ContactFormWithMugshot(data, file_data)</pre>
</div>
<p>In practice, you will usually specify <tt class="docutils literal"><span class="pre">request.FILES</span></tt> as the source
of file data (just like you use <tt class="docutils literal"><span class="pre">request.POST</span></tt> as the source of
form data):</p>
<div class="highlight-python"><pre># Bound form with an image field, data from the request
&gt;&gt;&gt; f = ContactFormWithMugshot(request.POST, request.FILES)</pre>
</div>
<p>Constructing an unbound form is the same as always -- just omit both
form data <em>and</em> file data:</p>
<div class="highlight-python"><pre># Unbound form with a image field
&gt;&gt;&gt; f = ContactFormWithMugshot()</pre>
</div>
<div class="section" id="s-testing-for-multipart-forms">
<span id="testing-for-multipart-forms"></span><h3>Testing for multipart forms<a class="headerlink" href="#testing-for-multipart-forms" title="Permalink to this headline">¶</a></h3>
<p>If you're writing reusable views or templates, you may not know ahead of time
whether your form is a multipart form or not. The <tt class="docutils literal"><span class="pre">is_multipart()</span></tt> method
tells you whether the form requires multipart encoding for submission:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">ContactFormWithMugshot</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">is_multipart</span><span class="p">()</span>
<span class="go">True</span>
</pre></div>
</div>
<p>Here's an example of how you might use this in a template:</p>
<div class="highlight-python"><pre>{% if form.is_multipart %}
    &lt;form enctype="multipart/form-data" method="post" action="/foo/"&gt;
{% else %}
    &lt;form method="post" action="/foo/"&gt;
{% endif %}
{{ form }}
&lt;/form&gt;</pre>
</div>
</div>
</div>
<div class="section" id="s-subclassing-forms">
<span id="subclassing-forms"></span><h2>Subclassing forms<a class="headerlink" href="#subclassing-forms" title="Permalink to this headline">¶</a></h2>
<p>If you have multiple <tt class="docutils literal"><span class="pre">Form</span></tt> classes that share fields, you can use
subclassing to remove redundancy.</p>
<p>When you subclass a custom <tt class="docutils literal"><span class="pre">Form</span></tt> class, the resulting subclass will
include all fields of the parent class(es), followed by the fields you define
in the subclass.</p>
<p>In this example, <tt class="docutils literal"><span class="pre">ContactFormWithPriority</span></tt> contains all the fields from
<tt class="docutils literal"><span class="pre">ContactForm</span></tt>, plus an additional field, <tt class="docutils literal"><span class="pre">priority</span></tt>. The <tt class="docutils literal"><span class="pre">ContactForm</span></tt>
fields are ordered first:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">ContactFormWithPriority</span><span class="p">(</span><span class="n">ContactForm</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">priority</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">CharField</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">ContactFormWithPriority</span><span class="p">(</span><span class="n">auto_id</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">f</span><span class="o">.</span><span class="n">as_ul</span><span class="p">()</span>
<span class="go">&lt;li&gt;Subject: &lt;input type="text" name="subject" maxlength="100" /&gt;&lt;/li&gt;</span>
<span class="go">&lt;li&gt;Message: &lt;input type="text" name="message" /&gt;&lt;/li&gt;</span>
<span class="go">&lt;li&gt;Sender: &lt;input type="text" name="sender" /&gt;&lt;/li&gt;</span>
<span class="go">&lt;li&gt;Cc myself: &lt;input type="checkbox" name="cc_myself" /&gt;&lt;/li&gt;</span>
<span class="go">&lt;li&gt;Priority: &lt;input type="text" name="priority" /&gt;&lt;/li&gt;</span>
</pre></div>
</div>
<p>It's possible to subclass multiple forms, treating forms as "mix-ins." In this
example, <tt class="docutils literal"><span class="pre">BeatleForm</span></tt> subclasses both <tt class="docutils literal"><span class="pre">PersonForm</span></tt> and <tt class="docutils literal"><span class="pre">InstrumentForm</span></tt>
(in that order), and its field list includes the fields from the parent
classes:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">PersonForm</span><span class="p">(</span><span class="n">Form</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">first_name</span> <span class="o">=</span> <span class="n">CharField</span><span class="p">()</span>
<span class="gp">... </span>    <span class="n">last_name</span> <span class="o">=</span> <span class="n">CharField</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">InstrumentForm</span><span class="p">(</span><span class="n">Form</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">instrument</span> <span class="o">=</span> <span class="n">CharField</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">BeatleForm</span><span class="p">(</span><span class="n">PersonForm</span><span class="p">,</span> <span class="n">InstrumentForm</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">haircut_type</span> <span class="o">=</span> <span class="n">CharField</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="n">BeatleForm</span><span class="p">(</span><span class="n">auto_id</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">b</span><span class="o">.</span><span class="n">as_ul</span><span class="p">()</span>
<span class="go">&lt;li&gt;First name: &lt;input type="text" name="first_name" /&gt;&lt;/li&gt;</span>
<span class="go">&lt;li&gt;Last name: &lt;input type="text" name="last_name" /&gt;&lt;/li&gt;</span>
<span class="go">&lt;li&gt;Instrument: &lt;input type="text" name="instrument" /&gt;&lt;/li&gt;</span>
<span class="go">&lt;li&gt;Haircut type: &lt;input type="text" name="haircut_type" /&gt;&lt;/li&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="s-prefixes-for-forms">
<span id="s-form-prefix"></span><span id="prefixes-for-forms"></span><span id="form-prefix"></span><h2>Prefixes for forms<a class="headerlink" href="#prefixes-for-forms" title="Permalink to this headline">¶</a></h2>
<dl class="attribute">
<dt id="django.forms.Form.prefix">
<!--[django.forms.Form.prefix]--><tt class="descclassname">Form.</tt><tt class="descname">prefix</tt><a class="headerlink" href="#django.forms.Form.prefix" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>You can put several Django forms inside one <tt class="docutils literal"><span class="pre">&lt;form&gt;</span></tt> tag. To give each
<tt class="docutils literal"><span class="pre">Form</span></tt> its own namespace, use the <tt class="docutils literal"><span class="pre">prefix</span></tt> keyword argument:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">mother</span> <span class="o">=</span> <span class="n">PersonForm</span><span class="p">(</span><span class="n">prefix</span><span class="o">=</span><span class="s">"mother"</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">father</span> <span class="o">=</span> <span class="n">PersonForm</span><span class="p">(</span><span class="n">prefix</span><span class="o">=</span><span class="s">"father"</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">mother</span><span class="o">.</span><span class="n">as_ul</span><span class="p">()</span>
<span class="go">&lt;li&gt;&lt;label for="id_mother-first_name"&gt;First name:&lt;/label&gt; &lt;input type="text" name="mother-first_name" id="id_mother-first_name" /&gt;&lt;/li&gt;</span>
<span class="go">&lt;li&gt;&lt;label for="id_mother-last_name"&gt;Last name:&lt;/label&gt; &lt;input type="text" name="mother-last_name" id="id_mother-last_name" /&gt;&lt;/li&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">father</span><span class="o">.</span><span class="n">as_ul</span><span class="p">()</span>
<span class="go">&lt;li&gt;&lt;label for="id_father-first_name"&gt;First name:&lt;/label&gt; &lt;input type="text" name="father-first_name" id="id_father-first_name" /&gt;&lt;/li&gt;</span>
<span class="go">&lt;li&gt;&lt;label for="id_father-last_name"&gt;Last name:&lt;/label&gt; &lt;input type="text" name="father-last_name" id="id_father-last_name" /&gt;&lt;/li&gt;</span>
</pre></div>
</div>
</div>
</div>



<div id="content-secondary">
  <h2 id="comments">Questions/Feedback</h2>
  <p>Having trouble? We'd like to help!</p>
  <ul>
    <li>
      Try the <a href="http://docs.djangoproject.com/en/dev/faq/">FAQ</a> — it's got answers to many common
      questions.
    </li>
    <li>
      Search for information in the <a href="http://groups.google.com/group/django-users/">archives of the
      django-users mailing list</a>, or <a href="http://groups.google.com/group/django-users/">post a question</a>.
    </li>
    <li>
      Ask a question in the <a href="irc://irc.freenode.net/">#django IRC
      channel</a>, or search the <a href="http://oebfare.com/logger/django/">IRC
      logs</a> to see if its been asked before.
    </li>
    <li>
      If you notice errors with this documentation, please <a href="http://code.djangoproject.com/simpleticket?component=Documentation">
      open a ticket</a> and let us know! Please only use the ticket tracker for
      criticisms and improvements on the docs. For tech support, use the
      resources above.
    </li>
  </ul>
</div>

		</div>
		<!-- END #content-main -->
		<div id="content-related" class="sidebar">
		
  
    <h2>Contents</h2>
    
      <ul>
<li><a class="reference external" href="">The Forms API</a><ul>
<li><a class="reference external" href="#bound-and-unbound-forms">Bound and unbound forms</a></li>
<li><a class="reference external" href="#using-forms-to-validate-data">Using forms to validate data</a><ul>
<li><a class="reference external" href="#behavior-of-unbound-forms">Behavior of unbound forms</a></li>
</ul>
</li>
<li><a class="reference external" href="#dynamic-initial-values">Dynamic initial values</a></li>
<li><a class="reference external" href="#accessing-clean-data">Accessing “clean” data</a></li>
<li><a class="reference external" href="#outputting-forms-as-html">Outputting forms as HTML</a><ul>
<li><a class="reference external" href="#as-p"><tt class="docutils literal"><span class="pre">as_p()</span></tt></a></li>
<li><a class="reference external" href="#as-ul"><tt class="docutils literal"><span class="pre">as_ul()</span></tt></a></li>
<li><a class="reference external" href="#as-table"><tt class="docutils literal"><span class="pre">as_table()</span></tt></a></li>
<li><a class="reference external" href="#configuring-html-label-tags">Configuring HTML <tt class="docutils literal"><span class="pre">&lt;label&gt;</span></tt> tags</a></li>
<li><a class="reference external" href="#notes-on-field-ordering">Notes on field ordering</a></li>
<li><a class="reference external" href="#how-errors-are-displayed">How errors are displayed</a></li>
<li><a class="reference external" href="#customizing-the-error-list-format">Customizing the error list format</a></li>
<li><a class="reference external" href="#more-granular-output">More granular output</a></li>
</ul>
</li>
<li><a class="reference external" href="#binding-uploaded-files-to-a-form">Binding uploaded files to a form</a><ul>
<li><a class="reference external" href="#testing-for-multipart-forms">Testing for multipart forms</a></li>
</ul>
</li>
<li><a class="reference external" href="#subclassing-forms">Subclassing forms</a></li>
<li><a class="reference external" href="#prefixes-for-forms">Prefixes for forms</a></li>
</ul>
</li>
</ul>

    
  
  
  
    <h2>Search</h2>
    
    <form action="/en/1.0/search/" id="search" class="search">
  <div>
    <input name="cx" value="009763561546736975936:e88ek0eurf4" type="hidden">
    <input name="cof" value="FORID:11" type="hidden">
    <input name="ie" value="UTF-8" type="hidden">
    <input name="hl" value="" type="hidden">
    <input style="background: rgb(255, 255, 255) url(http://www.google.com/coop/intl/en/images/google_custom_search_watermark.gif) no-repeat scroll left center; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;" id="id_search_q" class="query" name="q" type="text">
    <input name="sa" class="submit" value="Search" type="submit">
    <ul>
<li><label for="id_search_as_q_0"><input checked="checked" id="id_search_as_q_0" value="more:dev_docs" name="as_q" type="radio"> Latest</label></li>
<li><label for="id_search_as_q_1"><input id="id_search_as_q_1" value="more:1.0_docs" name="as_q" type="radio"> 1.0</label></li>
<li><label for="id_search_as_q_2"><input id="id_search_as_q_2" value="more:0.96_docs" name="as_q" type="radio"> 0.96</label></li>
<li><label for="id_search_as_q_3"><input id="id_search_as_q_3" value="more:all_docs" name="as_q" type="radio"> All</label></li>
</ul>
  </div>
</form>
<script type="text/javascript" src="Django%20%7C%20The%20Forms%20API%20%7C%20Django%20Documentation_files/brand.html"></script>
  
  
  
    <h2>Browse</h2>
    <ul>
      
        
          <li>Prev: <a href="http://docs.djangoproject.com/en/1.0/ref/forms/">Forms</a></li>
        
        
          <li>Next: <a href="http://docs.djangoproject.com/en/1.0/ref/forms/fields/">Form fields</a></li>
        
        <li><a href="http://docs.djangoproject.com/en/1.0/contents/">Table of contents</a></li>
        
          <li><a href="http://docs.djangoproject.com/en/1.0/genindex/">General Index</a></li>
        
          <li><a href="http://docs.djangoproject.com/en/1.0/modindex/">Global Module Index</a></li>
        
      
    </ul>
  
  
  
    <h2>You are here:</h2>
    <ul>
      
        <li>
          <a href="http://docs.djangoproject.com/en/1.0/">Django 1.0 documentation</a>
          
            <ul><li><a href="http://docs.djangoproject.com/en/1.0/ref/">API Reference</a>
          
            <ul><li><a href="http://docs.djangoproject.com/en/1.0/ref/forms/">Forms</a>
          
          <ul><li>The Forms API</li></ul>
          </li></ul></li></ul>
        </li>
      
    </ul>
  
  
  
    <h3>Last update:</h3>
    <div>May 23, 2009, 8:15 a.m. (<a href="http://www.timeanddate.com/worldclock/city.html?n=64">CDT</a>)</div>
  

		</div>
		<!-- END #content-related -->

		</div>
		<!-- END #content -->
		<div id="footer">
			<p>© 2005-2009 <a href="http://www.djangoproject.com/foundation/">Django Software Foundation</a> unless otherwise noted. Django is a registered trademark of the Django Software Foundation. 
			Hosting graciously provided by <a href="http://mediatemple.net/">
			<img style="vertical-align: middle; position: relative; top: -1px;" src="Django%20%7C%20The%20Forms%20API%20%7C%20Django%20Documentation_files/mt.png" alt="media temple"></a>
			</p>
		</div>
		<!-- END #footer -->
	</div>
	<!-- END #container -->
	</body></html>